<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestAddItemPage extends PHPUnit_Framework_TestCase {
	
	function test_page_inc_check_access() {
		$page = new Page();

		$add_item_page = new AddItemPage();
		$mock_access = $this->getMock('AccessManager');
		$mock_access->expects($this->once())
				->method('check_access')
				->with('READ');
		$add_item_page->set_access_manager($mock_access);

		$add_item_page->page_inc_check_access($page);
	}

	function test_page_inc_execute_action() {
		$expected_action = "some action that's going to be ignored anyway";

		$page = new Page();

		$add_item_page = new AddItemPage();

		$mock_alerter = $this->getMock('Alerter');
		$mock_alerter->expects($this->once())
				->method('add_alert')
				->with('unknown action');
		$add_item_page->set_alerter($mock_alerter);

		$add_item_page->page_inc_execute_action($expected_action);
	}

	function test_page_inc_execute() {
		$expected_dir = array();
		$expected_exception = new exception("fumigated");

		$page = new page();
		$page->dir_depth = 2;
		$page->directory_tree[1] = $expected_dir;

		$add_item_page = new additempage();

		$mock_toolbox = $this->getmock('Toolbox');
		$mock_toolbox->expects($this->once())
				->method('endpage')
				->with($page, 'Hier kunnen geen items toegevoegd worden')
				->will($this->throwexception($expected_exception));
		$add_item_page->set_toolbox($mock_toolbox);

		try {
			$add_item_page->page_inc_execute($page);
		} catch (exception $ex) {
			//ignore
		}
	}

	function test_page_inc_execute_with_show_items() {
		$expected_dir = array('show_items' => true);
		$expected_title = "Een item toevoegen";

		$page = new page();
		$page->dir_depth = 2;
		$page->directory_tree[1] = $expected_dir;

		$add_item_page = new additempage();

		$mock_toolbox = $this->getmock('Toolbox');
		$mock_toolbox->expects($this->never())
				->method('endpage');
		$add_item_page->set_toolbox($mock_toolbox);

		$mock_item_functions = $this->getMock('ItemFunctions');
		$mock_item_functions->expects($this->once())
				->method('item_form')
				->with($this->isInstanceOf('Page'));
		$add_item_page->set_item_functions($mock_item_functions);

		$add_item_page->page_inc_execute($page);

		$this->assertEquals($expected_title, $page->content['title']);
	}
}


?>
